/**
 * SpringMVC restful 风格
 * 用户控制器，控制关于实名认证审核的一些业务逻辑操作
 * @author WangXinLei
 * @date 2015-03-06
 */
package com.chnbst.bstyun.controller.admin;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.Area;
import com.chnbst.bstyun.model.pojo.Attach;
import com.chnbst.bstyun.model.pojo.City;
import com.chnbst.bstyun.model.pojo.CompanyIdentify;
import com.chnbst.bstyun.model.pojo.PersonalIdentify;
import com.chnbst.bstyun.model.pojo.Province;
import com.chnbst.bstyun.model.vo.CompanyIdentifyVo;
import com.chnbst.bstyun.model.vo.PersonalIdentifyVo;
import com.chnbst.bstyun.service.AttachService;
import com.chnbst.bstyun.service.CompanyIdentifyService;
import com.chnbst.bstyun.service.PersonalIdentifyService;
import com.chnbst.bstyun.service.PlaceService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author WangXinLei
 *
 */

@Controller
@RequestMapping("/sysAdmin")
public class CertificateAdminController {
	
	@Autowired
	private PersonalIdentifyService personalIdentifyService;
	@Autowired
	private CompanyIdentifyService companyIdentifyService;
	@Autowired
	private PlaceService placeService;
	@Autowired
	private AttachService attachService;
	
	/**
	 * @return 企业实名认证列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/certificateAdmin/cCertificateList",method=RequestMethod.GET)
	public ModelAndView cCertificateList()throws Exception{
		ModelAndView mav = new ModelAndView();
		List<CompanyIdentifyVo> companyIdentifyList = companyIdentifyService.selectList();
		mav.addObject("companyIdentifyList", companyIdentifyList);
		mav.setViewName("admin/userManager/cCertificateManager");
		return mav;
	}
	
	/**
	 * @return 企业实名认证详情页面
	 * @throws Exception
	 */
	@RequestMapping(value="/certificateAdmin/cCertificateDetail/{id}",method=RequestMethod.GET)
	public ModelAndView cCertificateDetail(@PathVariable String id)throws Exception{
		ModelAndView mav = new ModelAndView();
		CompanyIdentify companyIdentify = companyIdentifyService.selectById(id);
		
		Province province = placeService.getProvinceByPcode(companyIdentify.getBnessLicenseProvince());
		City city = placeService.getCityByCcode(companyIdentify.getBnessLicenseCity());
		Area area = placeService.getAreaByAcode(companyIdentify.getBnessLicenseArea());
		Attach authLicense = attachService.selectAttachByAttachId(companyIdentify.getBnessLicenseUrl());
		Attach duplicateLicense = attachService.selectAttachByAttachId(companyIdentify.getBnessStampUrl());
		
		mav.addObject("province", province);
		mav.addObject("city", city);
		mav.addObject("area", area);
		mav.addObject("authLicense", authLicense);
		mav.addObject("duplicateLicense", duplicateLicense);
		mav.addObject("companyIdentify",companyIdentify);
		mav.setViewName("admin/userManager/cCertificateDetail");
		return mav;
	}
	
	/**
	 * @return 企业实名认证审核页面
	 * @throws Exception
	 */
	@RequestMapping(value="/certificateAdmin/cCertificateDetail/{id}/{state}",method=RequestMethod.GET)
	public ModelAndView cCertificateAudit(@PathVariable String id,@PathVariable Integer state)throws Exception{
		ModelAndView mav = new ModelAndView();
		CompanyIdentify companyIdentify = companyIdentifyService.selectById(id);
		companyIdentify.setAuditTime(new Date());
		companyIdentify.setState(state);
		companyIdentifyService.updateById(companyIdentify);

		List<CompanyIdentifyVo> companyIdentifyList = companyIdentifyService.selectList();
		mav.addObject("companyIdentifyList", companyIdentifyList);
		mav.addObject("state", 0);
		mav.setViewName("redirect:/sysAdmin/certificateAdmin/cCertificateList");
//		mav.setViewName("admin/userManager/cCertificateManager");
		return mav;
	}
	
	/**
	 * @return 企业实名认证列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/certificateAdmin/cCertificateList/{state}",method=RequestMethod.GET)
	public ModelAndView cCertificateListByCondition(@PathVariable Integer state)throws Exception{
		ModelAndView mav = new ModelAndView();
		List<CompanyIdentifyVo> companyIdentifyList = companyIdentifyService.selectListByCondition(state);
		mav.addObject("companyIdentifyList", companyIdentifyList);
		mav.addObject("state", state);
		mav.setViewName("admin/userManager/cCertificateManager");
		return mav;
	}
	
	/**
	 * @return 个人实名认证列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/certificateAdmin/pCertificateList",method=RequestMethod.GET)
	public ModelAndView pCertificateList()throws Exception{
		ModelAndView mav = new ModelAndView();
		List<PersonalIdentifyVo> personalIdentifyList = personalIdentifyService.selectList();
		mav.addObject("personalIdentifyList",personalIdentifyList);
		mav.setViewName("admin/userManager/pCertificateManager");
		return mav;
	}
	
	/**
	 * @return 个人实名认证详情页面
	 * @throws Exception
	 */
	@RequestMapping(value="/certificateAdmin/pCertificateDetail/{id}",method=RequestMethod.GET)
	public ModelAndView pCertificateDetail(@PathVariable String id)throws Exception{
		ModelAndView mav = new ModelAndView();
		PersonalIdentify personalIdentify = personalIdentifyService.selectById(id);
		mav.addObject("personalIdentify",personalIdentify);
		mav.setViewName("admin/userManager/pCertificateDetail");
		return mav;
	}
	
	/**
	 * @return 个人实名认证审核页面
	 * @throws Exception
	 */
	@RequestMapping(value="/certificateAdmin/pCertificateDetail/{id}/{state}",method=RequestMethod.GET)
	public ModelAndView pCertificateAudit(@PathVariable String id,@PathVariable Integer state)throws Exception{
		ModelAndView mav = new ModelAndView();
		PersonalIdentify personalIdentify = personalIdentifyService.selectById(id);
		personalIdentify.setAuditTime(new Date());
		personalIdentify.setState(state);
		personalIdentifyService.updateById(personalIdentify);

		List<PersonalIdentifyVo> personalIdentifyList = personalIdentifyService.selectList();
		mav.addObject("personalIdentifyList", personalIdentifyList);
		mav.addObject("state", 0);
		mav.setViewName("redirect:/sysAdmin/certificateAdmin/pCertificateList");
//		mav.setViewName("admin/userManager/pCertificateManager");
		return mav;
	}
	
	/**
	 * @return 个人实名认证列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/certificateAdmin/pCertificateList/{state}",method=RequestMethod.GET)
	public ModelAndView pCertificateListByCondition(@PathVariable Integer state)throws Exception{
		ModelAndView mav = new ModelAndView();
		List<PersonalIdentifyVo> personalIdentifyList = personalIdentifyService.selectListByCondition(state);
		mav.addObject("personalIdentifyList", personalIdentifyList);
		mav.addObject("state", state);
		mav.setViewName("admin/userManager/pCertificateManager");
		return mav;
	}
}
